<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__location}">
      <div class="ba-operate-form">
        <el-form :model="dataForm" @keyup.enter.native="getDataList()" label-width="70px">
          <el-row :gutter="20">
            <el-col :md="6" :sm="6">
              <el-form-item label="设备编号">
                <el-input v-model="dataForm.dvcNo" placeholder="精确匹配" maxlength="20" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="6">
              <el-form-item label="货位编号">
                <el-input v-model="dataForm.locateNo" placeholder="模糊匹配" maxlength="20" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="6">
              <el-form-item label="托盘条码">
                <el-input v-model="dataForm.contNo" placeholder="精确匹配" maxlength="32" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="6">
              <div class="right-handle-group">
                <el-button-group>
                  <el-button @click="getDataList()" type="primary">{{ $t('query') }}</el-button>
                  <el-button @click="getReset()">{{ $t("reset") }}</el-button>
                  <el-tooltip class="item" effect="dark" content="高级搜索" placement="top-start">
                    <el-button @click="advanced = !advanced" color="#dcdfe6" icon="el-icon-search" plain></el-button>
                  </el-tooltip>
                </el-button-group>
              </div>
            </el-col>
          </el-row>
          <el-collapse-transition>
            <div v-show="advanced">
              <el-row :gutter="20">
                <el-col :md="6" :sm="9">
                  <el-form-item label="货位状态">
                    <el-select v-model="dataForm.locateSt" placeholder="相关状态">
                      <el-option label="空闲" value="0"></el-option>
                      <el-option label="预分配" value="1"></el-option>
                      <el-option label="已使用" value="2"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :md="6" :sm="9">
                  <el-form-item label="是否可用">
                    <el-select v-model="dataForm.active" placeholder="可用状态">
                      <el-option label="可用" value="0"></el-option>
                      <el-option label="禁用" value="1"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-collapse-transition>
        </el-form>
      </div>

      <el-table id="tableData" v-loading="dataListLoading" :data="dataList" border
        @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column prop="locateId" label="id" header-align="center" align="center" sortable></el-table-column>
        <el-table-column prop="dvcNo" label="设备编号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="locateNo" label="货位编号" header-align="center" align="center" min-width="135" sortable>
        </el-table-column>
        <el-table-column prop="locateSt" label="货位状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.locateSt === 0">空闲</el-tag>
            <el-tag v-else-if="scope.row.locateSt === 1" type="warning">预分配</el-tag>
            <el-tag v-else-if="scope.row.locateSt === 2" type="success">已使用</el-tag>
            <el-tag v-else type="danger">异常</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="active" label="可用状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.active" @change="changeStatus($event, scope.row)" active-color="#13ce66"
              inactive-color="#ff4949" :active-value="0" :inactive-value="1"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="contNo" label="托盘条码" header-align="center" align="center" min-width="220"></el-table-column>
        <el-table-column prop="comments" label="备注" header-align="center" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="updaterName" label="更新用户" header-align="center" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="updateDate" label="更新时间" header-align="center" align="center" min-width="155" sortable>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import debounce from 'lodash/debounce'
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './location-add-or-update'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/wms/location/page',
        getDataListIsPage: true,
        deleteURL: '/wms/location',
        deleteIsBatch: true,
      },
      dataForm: {
        dvcNo: '',
        locateNo: '',
        contNo: '',
        locateSt: '',
        active: '',
      },
      changeForm: {
        locateId: '',
        active: ''
      },
      advanced: false, //高级搜索
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
     //0.重置搜索框
     getReset() {
      this.dataForm = this.$options.data().dataForm;
    },
    changeStatus: debounce(function (val, row) {
      var tempStatus = row.active === 0 ? 1 : 0;
      this.changeForm.locateId = row.locateId;
      this.changeForm.active = row.active;
      this.$http[!this.changeForm.locateId ? 'post' : 'put']('/wms/location/', this.changeForm).then(({ data: res }) => {
        if (res.code !== 0) {
          //如果更新失败则重置为原来的状态
          row.active = tempStatus;
          return this.$message.error(res.msg)
        }
        this.$message({
          message: this.$t('prompt.success'),
          type: 'success',
          duration: 500,
          onClose: () => {
          }
        })
      }).catch(() => { })
    })
  }
}
</script>
<style>
.el-row {
  margin-bottom: 0px;
}

.right-handle-group {
  float: right;
}
</style>